﻿using BwTMusicModels;
using BwTServices.Singer;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            SingerServices singer = new SingerServices();
            var lists=  singer.GetAllSIngers();

            for (int i = 0; i < lists.Count; i++)
            {
                var ss = HttpSingerId(lists[i].SingerName, "{}", "post");
                var sss = JsonConvert.DeserializeObject<models>(ss).relative;
                if(sss.singer!=null )
                {
                    var ids = sss.singer[0].singerid;
                    var aa = HttpSingerInfo(ids, "{}", "post");
                    var aaa = JsonConvert.DeserializeObject<info>(aa);
                    North_Singer north = new North_Singer();
                    north.SingerDesc = aaa.data.intro;
                    north.SingerPictrue = aaa.data.imgurl.Replace("{size}", "240");
                    singer.ModifySingerInfo(north, lists[i].SingerName);
                }
                Console.WriteLine($"完成第{i+1}个,共{lists.Count}个");
            }
            var baidulist = singer.GetAllBaiduSingers();
            for (int i = 0; i < baidulist.Count; i++)
            {
                var ss = BaiduHttpSingerId(baidulist[i].SingerName, "{}", "get");
                var sss = JsonConvert.DeserializeObject<baidumodels>(ss);
                if (sss.result != null)
                {
                    if (sss.result.artist_info != null)
                    {
                        if (sss.result.artist_info.artist_list != null)
                        {

                            for (int j = 0; j < sss.result.artist_info.artist_list.Count; j++)
                            {
                                var names = sss.result.artist_info.artist_list[j].author.Replace("<em>", "").Replace("</em>", "");
                                if (baidulist[i].SingerName.Contains(names))
                                {
                                    var ids = sss.result.artist_info.artist_list[j].artist_id;
                                    var aa = BaiduHttpSingerInfo(ids, "{}", "get");
                                    var aaa = JsonConvert.DeserializeObject<singerinfos>(aa);
                                    North_Singer north = new North_Singer();
                                    north.Company = aaa.company;
                                    north.SingerBir = aaa.birth;
                                    north.SingerFName = aaa.firstchar;
                                    north.Constellation = aaa.constellation;
                                    north.SingerSex = Convert.ToInt32(aaa.gender==""? 0: Convert.ToInt32(aaa.gender));
                                    singer.ModifyBaiduSingerInfo(north, baidulist[i].SingerName);
                                    break;
                                }
                            }
                        }
                    }
                }
                Console.WriteLine($"完成第{i + 1}个,共{baidulist.Count}个");
            }
        }

        public static string HttpSingerId(string name, string jsonstr, string type)
        {
            string url = "http://mobilecdn.kugou.com/api/v3/search/song?format=json&keyword="+name+"&page=1&pagesize=1&showtype=2";
            Encoding encoding = Encoding.UTF8;
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);//webrequest请求api地址  
            request.Accept = "text/html,application/xhtml+xml,*/*";
            request.ContentType = "application/json";
            request.Method = type.ToUpper().ToString();//get或者post  
            byte[] buffer = encoding.GetBytes(jsonstr);
            request.ContentLength = buffer.Length;
            request.GetRequestStream().Write(buffer, 0, buffer.Length);
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
            {
                return reader.ReadToEnd();
            }
        }
        public static string HttpSingerInfo(string name, string jsonstr, string type)
        {
            string url = "http://mobilecdnbj.kugou.com/api/v3/singer/info?singerid="+name+"&with_res_tag=2";
            Encoding encoding = Encoding.UTF8;
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);//webrequest请求api地址  
            request.Accept = "text/html,application/xhtml+xml,*/*";
            request.ContentType = "application/json";
            request.Method = type.ToUpper().ToString();//get或者post  
            byte[] buffer = encoding.GetBytes(jsonstr);
            request.ContentLength = buffer.Length;
            request.GetRequestStream().Write(buffer, 0, buffer.Length);
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
            {
                return reader.ReadToEnd();
            }
        }

        public static string BaiduHttpSingerId(string name, string jsonstr, string type)
        {
            string url = "http://tingapi.ting.baidu.com/v1/restserver/ting?from=android&version=5.6.5.0&method=baidu.ting.search.merge&format=json&query="+name+"&page_no=1&page_size=20&type=-1&use_cluster=1";
            Encoding encoding = Encoding.UTF8;
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);//webrequest请求api地址  
            request.Accept = "text/html,application/xhtml+xml,*/*";
            request.ContentType = "application/json";
            request.Method = type.ToUpper().ToString();//get或者post  
            //byte[] buffer = encoding.GetBytes(jsonstr);
            //request.ContentLength = buffer.Length;
            //request.GetRequestStream().Write(buffer, 0, buffer.Length);
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
            {
                return reader.ReadToEnd();
            }
        }
        public static string BaiduHttpSingerInfo(string name, string jsonstr, string type)
        {
            string url = "http://music.taihe.com/data/tingapi/v1/restserver/ting?method=baidu.ting.artist.getInfo&artistid="+name+"&from=web";
            Encoding encoding = Encoding.UTF8;
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);//webrequest请求api地址  
            request.Accept = "text/html,application/xhtml+xml,*/*";
            request.ContentType = "application/json";
            //request.Method = type.ToUpper().ToString();//get或者post  
            //byte[] buffer = encoding.GetBytes(jsonstr);
            //request.ContentLength = buffer.Length;
            //request.GetRequestStream().Write(buffer, 0, buffer.Length);
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
            {
                return reader.ReadToEnd();
            }
        }
    }
}
